<?php
require_once 'BaseController.php';

/**
 * 超かんたん物件リクエスト検索クラス<br />
 *
 * @package   apamanshop
 * @author    H.Imamura
 * @copyright (c) 2012 SystemSoft Corp. All rights reserved
 */
class Search_EasyrequestController extends Search_BaseController
{
	//protected $_breadcrumb = null;

	/**
	 * 初期処理（オーバーライド）
	 */
	public function preDispatch()
	{
		// 初期設定
		$this->_display_path      = "";
		$this->_main_helper       = null;
		$this->_SrhKbn            = "";
		$this->_SrhType           = "";
		$this->_controller        = "";
		
		$this->_baseurl    = $this->_config['apm']['baseurl'];
		$this->_aplbaseurl = $this->_config['apm']['apl']['static']['baseurl'];
		
		// 共通モデル設定
		$this->_model_common = new Search_Model_Common();
		
		// パラメータ取得
		$this->_params = $this->getParameters();
		// パラメータ設定
		$this->setParams();
		
		// 表示テンプレート
		$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/easyrequest.tpl';
		$this->setMainTemplate('cms_easy.tpl');
	}
	
	/**
	 * 後処理（オーバーライド）
	 */
	public function postDispatch()
	{
		$this->_data['SrhKbn']  = $this->_SrhKbn;
		$this->_data['SrhType'] = $this->_SrhType;
		
		// パンくずセット
		$this->_breadcrumb->reset();
		$this->_breadcrumb->add(Apm_Config_Search::BREADCRUMB_TOP, $this->_baseurl);
		$this->_breadcrumb->add('おまかせ物件リクエスト', $this->_baseurl.'info/request/');
		if ($this->_SrhType == Apm_Config_Search::SRHTYPE_JYUSYO) {
			$this->_breadcrumb->add('かんたんリクエスト', $this->_baseurl.'search/easyrequest/jusyo/');
		} else {
			$this->_breadcrumb->add('かんたんリクエスト', $this->_baseurl.'search/easyrequest/');
		}
		$this->setBreadCrumb($this->_breadcrumb->generateBreadcrumb());
		
		// ブロック設定
		if (!empty($this->_main_helper)) {
			$this->_data['main_block'] = $this->_main_helper->getBlock($this->_data);
		}
		// 表示
		$this->display($this->_display_path);
	}
	
	/**
	 * 超かんたんリクエスト画面
	 */
	public function indexAction()
	{
		// グローバルナビセッションクリア
		Apm_Session_GlobalNavi::clear();
		
		/*
		 * 基本情報設定
		 */
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_BUKENREQUEST, "easyrequest");
		$this->_SrhType = Apm_Config_Search::SRHTYPE_ENSENEKI;
		// JS
		$this->addMetaJs('/app/js/jquery.js');
		$this->addMetaJs('/app/js/search/Easyrequest.js');
		// CSS
		$this->addMetaCss('/css/common/add.css',array('type' => 'text/css','media' => 'all'));
		// テンプレート
		$this->_main_helper = $this->getApmHelper('easyrequest_ensen.tpl');
		// hiddenタグ設定
		$this->setHiddenTags();
		
		// 沿線検索
		$this->selEnsen();
		
	}
	
	/**
	 * 超かんたんリクエスト画面（市区町村選択）
	 */
	public function jusyoAction()
	{
		// グローバルナビセッションクリア
		Apm_Session_GlobalNavi::clear();
		
		/*
		 * 基本情報設定
		 */
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_BUKENREQUEST, "easyrequest");
		$this->_SrhType = Apm_Config_Search::SRHTYPE_JYUSYO;
		// JS
		$this->addMetaJs('/app/js/jquery.js');
		$this->addMetaJs('/app/js/search/Easyrequest.js');
		// CSS
		$this->addMetaCss('/css/common/add.css',array('type' => 'text/css','media' => 'all'));
		// テンプレート
		$this->_main_helper = $this->getApmHelper('easyrequest_city.tpl');
		// hiddenタグ設定
		$this->setHiddenTags();
		
		// 市区町村検索
		$this->selCity();
		
	}
	
	/**
	 * 沿線検索API
	 */
	public function ensenAction()
	{
		$html = '';
		
		// 沿線CD破棄
		if (isset($this->_params['RailCd'])) {
			unset($this->_params['RailCd']);
		}
		// 駅CD破棄
		if (isset($this->_params['StCd'])) {
			unset($this->_params['StCd']);
		}
		
		// 沿線検索
		$this->selEnsen();
		
		// メインブロック生成
		$this->_main_helper = $this->getApmHelper('easyrequest_ensen.tpl');
		$html  = $this->_main_helper->getBlock($this->_data);
		$html .= "<>";
		$html .= $this->_data['eki_block'];
		
		// 返還
		Apm_Util::outputXPassThroutgh();
		echo $html;
		exit;
	}
	
	/**
	 * 駅検索API
	 */
	public function ekiAction()
	{
		$html = '';
		
		// 駅CD破棄
		if (isset($this->_params['StCd'])) {
			unset($this->_params['StCd']);
		}
		
		// 駅検索
		$this->selEki();
		$html = $this->_data['eki_block'];
		
		// 返還
		Apm_Util::outputXPassThroutgh();
		echo $html;
		exit;
	}
	
	/**
	 * 市区町村検索API
	 */
	public function cityAction()
	{
		$html = '';
		
		// 市区町村CD破棄
		if (isset($this->_params['CityCd'])) {
			unset($this->_params['CityCd']);
		}
		
		// 市区町村検索
		$this->selCity();
		
		// メインブロック生成
		$this->_main_helper = $this->getApmHelper('easyrequest_city.tpl');
		$html = $this->_main_helper->getBlock($this->_data);
		
		// 返還
		Apm_Util::outputXPassThroutgh();
		echo $html;
		exit;
	}
	
	/**
	 * デフォルトパラメータ設定
	 */
	public function setParams()
	{
		// 都道府県CD
		if (!empty($this->_params['PrefCd'])) {
			$this->_data['PrefCd'] = array();
			if (!is_array($this->_params['PrefCd'])) {
				// 配列化
				$pref = $this->_params['PrefCd'];
				$this->_params['PrefCd'] = array();
				$this->_params['PrefCd'][] = $pref;
			}
			foreach ($this->_params['PrefCd'] as $value) {
				$this->_data['PrefCd'][$value] = 'on';
			}
		} else {
			$this->_data['PrefCd']['00'] = 'on';
		}
		
		// 沿線CD
		if (!empty($this->_params['RailCd'])) {
			$this->_data['RailCd'] = array();
			if (!is_array($this->_params['RailCd'])) {
				// 配列化
				$rail = $this->_params['RailCd'];
				$this->_params['RailCd'] = array();
				$this->_params['RailCd'][] = $rail;
			}
			foreach ($this->_params['RailCd'] as $value) {
				$this->_data['RailCd'][$value] = 'on';
			}
		} else {
			$this->_data['RailCd']['00'] = 'on';
		}
		
		// 駅CD
		if (!empty($this->_params['StCd'])) {
			$this->_data['StCd'] = array();
			if (!is_array($this->_params['StCd'])) {
				// 配列化
				$st = $this->_params['StCd'];
				$this->_params['StCd'] = array();
				$this->_params['StCd'][] = $st;
			}
			foreach ($this->_params['StCd'] as $value) {
				$this->_data['StCd'][$value] = 'on';
			}
		} else {
			$this->_data['StCd']['00'] = 'on';
		}
		
		// 市区町村CD
		if (!empty($this->_params['CityCd'])) {
			$this->_data['CityCd'] = array();
			if (!is_array($this->_params['CityCd'])) {
				// 配列化
				$rail = $this->_params['CityCd'];
				$this->_params['CityCd'] = array();
				$this->_params['CityCd'][] = $rail;
			}
			foreach ($this->_params['CityCd'] as $value) {
				$this->_data['CityCd'][$value] = 'on';
			}
		} else {
			$this->_data['CityCd']['00'] = 'on';
		}
		
		// 基本条件
		$this->_model_basic = new Search_Model_Basicjyoken();
		$basic = $this->_model_basic->setChecked($this->_params);
		$this->_data['basic'] = array();
		$this->_data['basic']['madori0Checked'] = 'selected';
		foreach ($basic as $key => $val) {
			if ($val == 'selected' || $val == 'checked') {
				$this->_data['basic'][$key] = 'selected';
				if (strpos($key, 'madori') === 0) {
					$this->_data['basic']['madori0Checked'] = '';
				}
			}
		}
		
		// その他コメント
		$this->_data['Comment'] = $this->_params['Comment'];
	}
	
	/**
	 * hiddenタグ設定
	 */
	public function setHiddenTags()
	{
		// hidden設定するパラメータ
		$params_list = array(
			  'syokihiyo', 'tinryo_jouken2', 'tyusyajyo_komi', 'shikikin_nashi', 'reikin_nashi'
			, 'freerent', 'heyawari', 'jyohou'
			);
		$params_arr_list = array(
			  'campaign', 'syubetu', 'kodawari'
			);
		
		// 必要パラメータのみ抽出
		$hiddenParams = array();
		foreach ($this->_params as $key => $val) {
			if (in_array($key, $params_list)) {
				$hiddenParams[$key] = $val;
			}
			foreach ($params_arr_list as $arrKey) {
				if (strpos($arrKey, $key, 0) === 0) {
					$hiddenParams[$key] = $val;
				}
			}
		}
		// hiddenタグ生成
		$this->_data['hiddenTag'] = Apm_Util::generateHiddenTag($hiddenParams);
		// uriParams生成
		$uriParams = "";
		foreach ($hiddenParams as $key => $val) {
			if (is_array($val)) {
				foreach ($val as $val_key => $val_val) {
					$uriParams .= '&' . $key . '['.$val_key.']=' . $val_val;
				}
			} else {
				$uriParams .= '&' . $key . '=' . $val;
			}
		}
		if ($uriParams != "") {
			// 先頭の&を?へ変更
			$uriParams = '?' . substr($uriParams, 1);
		}$this->_data['hiddenUri'] = $uriParams;
		
	}
	
	/**
	 * 沿線検索
	 */
	public function selEnsen()
	{
		// 都道府県CDが存在するか
		if (!empty($this->_params['PrefCd'])) {
			// 沿線検索
			$this->_model_ensen = new Search_Model_Selensen();
			$data['selensen'] = array();
			$data['selensen'] = $this->_model_ensen->getRequestEnsenDataByMulti($this->_params);
			// 表示用データに変換
			$data['Mapping']  = array();
			$this->_model_ensen->convertEnsenDataByMulti($data);
			$this->_data['ensenData'] = $data['Mapping'];
		} else {
			$this->_data['ensenData'] = null;
		}
		
		// 駅検索
		$this->selEki();
	}
	
	/**
	 * 駅検索
	 */
	public function selEki()
	{
		// 沿線CDが存在するか
		if (!empty($this->_params['RailCd'])) {
			// 駅検索
			$this->_model_eki = new Search_Model_Seleki();
			$data['seleki'] = array();
			$data['seleki'] = $this->_model_eki->getRequestEkiData($this->_params);
			// 表示用データに変換
			$data['Mapping']  = array();
			$this->_model_eki->convertEkiData($data);
			$this->_data['ekiData'] = $data['Mapping'];
		} else {
			$this->_data['ekiData'] = null;
		}
		// 駅ブロック生成
		$this->_data['eki_block'] = $this->getApmHelper('easyrequest_eki.tpl')->getBlock($this->_data);
	}
	
	/**
	 * 市区町村検索
	 */
	public function selCity()
	{
		// 都道府県CDが存在するか
		if (!empty($this->_params['PrefCd'])) {
			// 市区町村検索
			$this->_model_sikucyoson = new Search_Model_Selsikucyoson();
			$data['selcity'] = array();
			$data['selcity'] = $this->_model_sikucyoson->getRequestShikucyosonData($this->_params);
			// 表示用データに変換
			$data['Mapping']  = array();
			$this->_model_sikucyoson->convertShikucyosonData($data);
			$this->_data['cityData'] = $data['Mapping'];
		} else {
			$this->_data['cityData'] = null;
		}
	}
	
}
?>